package desktopAPI.integration;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

public class DesktopIntegration {
    static Desktop desktop;

    public static void main(String[] args) {

        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        } else {
            System.out.println("Desktop class is not supported");
            System.exit(1);
        }


        JFrame frame = new JFrame();
        frame.setTitle("java 6 Desktop Integration");
        frame.setSize(400, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        JButton openItem = new JButton("Open");
        JButton editItem = new JButton("Edit");
        JButton printItem = new JButton("Print");
        JButton browseToItem = new JButton("Browse");
        JButton mailToItem = new JButton("Mail");

        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.add(openItem);
        frame.add(editItem);
        frame.add(printItem);
        frame.add(browseToItem);
        frame.add(mailToItem);

        //setting look and feel, just to make it nice to the eye.
        try {
            UIManager.setLookAndFeel(new WindowsLookAndFeel());
        } catch (Exception e) {}


        openItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    try {
                        desktop.open(chooser.getSelectedFile().getAbsoluteFile());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        editItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    try {
                        desktop.edit(chooser.getSelectedFile().getAbsoluteFile());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        printItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    try {
                        desktop.print(chooser.getSelectedFile().getAbsoluteFile());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        browseToItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    URI browseURI = new URI("www.Pavel-Kaminsky.com");
                    desktop.browse(browseURI);
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        });

        mailToItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    URI mailURI = new URI("mailto:webmaster@pavel-kaminsky.com");
                    desktop.mail(mailURI);
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        });

    }
}